package com.seamtop.cuber.server.job;

import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.entriy.CuberCounter;
import com.seamtop.cuber.common.entriy.CuberCounterSet;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.entriy.Duration;
import com.seamtop.cuber.common.util.DateUtil;
import com.seamtop.cuber.common.util.StringUtil;

import java.util.Date;
import java.util.Random;

/**
 * Created by feng on 2015/9/28.
 */
public class JobKits {

    public JobKits(){

    }

    public static CuberReducer getCronReducerOfCounter(CuberCounter cuberCounter){
        CuberReducer reducer = new CuberReducer();
        CuberCounterSet cuberCounterSet = DataObject.getCuberCounterSetMap().get(cuberCounter.getCounterSetId());
        String cronExpression = JobKits.getCrontabExpress(cuberCounterSet.getCounterSettings().getTimeStatPeriod());
        reducer.setCronExpression(cronExpression);
        reducer.setStatPeriod(cuberCounterSet.getCounterSettings().getTimeStatPeriod());
        reducer.setOutPutTable("CUBER_STAT_RESULT");
        reducer.setStatType("sum");
        reducer.setStatColumn("value");
        reducer.setReducerId(cuberCounter.getCounterId());
        reducer.setSourceTable(cuberCounter.getCounterName());
        reducer.setFilterDateColumn("key");
        return reducer;
    }

    public static String getCrontabExpress(String period){
        String cronExpression = null;
        if(period.startsWith("every")){
            String[] recentyArray = period.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            int step = Integer.parseInt(statSteps);
            if("minute".equals(statUnits)){
                int t = step > 10?step:10;
                cronExpression = "1 1/"+t+" * * * ? ";
            }else if("hour".equals(statUnits)){
                cronExpression = "1 1 1/"+step+ " * * ? ";
            }else if("day".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("week".equals(statUnits)){
                cronExpression = "0 0 1 ? * 1/"+step;
            }else if("month".equals(statUnits)){
                cronExpression = "0 0 1 1/"+step+" * ?";
            }
        }else if(period.startsWith("recenty")){
            String[] recentyArray = period.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            int step = Integer.parseInt(statSteps);
            if("minute".equals(statUnits)){
                int t = step > 10?step:10;
                cronExpression = "1/"+t+" * *  * * ? ";
            }else if("hour".equals(statUnits)){
                cronExpression = "1 1/"+step+" * * * ? ";
            }else if("day".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("week".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("month".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }
        }
        cronExpression = "1 * * * * ? ";
        return cronExpression;
    }

    public static String getCrontabExpress(CuberReducer reducer){
        String cronExpression = reducer.getCronExpression();
        if(!StringUtil.isEmpty(cronExpression)){
            return cronExpression;
        }
        String statPeriod = reducer.getStatPeriod();
        if(statPeriod.startsWith("every")){
            String[] recentyArray = statPeriod.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            int step = Integer.parseInt(statSteps);
            if("minute".equals(statUnits)){
                int t = step > 10?step:10;
                cronExpression = "1 1/"+t+" * * * ? ";
            }else if("hour".equals(statUnits)){
                cronExpression = "1 1 1/"+step+ " * * ? ";
            }else if("day".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("week".equals(statUnits)){
                cronExpression = "0 0 1 ? * 1/"+step;
            }else if("month".equals(statUnits)){
                cronExpression = "0 0 1 1/"+step+" * ?";
            }
        }else if(statPeriod.startsWith("recenty")){
            String[] recentyArray = statPeriod.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            int step = Integer.parseInt(statSteps);
            if("minute".equals(statUnits)){
                int t = step > 10?step:10;
                cronExpression = "1/"+t+" * *  * * ? ";
            }else if("hour".equals(statUnits)){
                cronExpression = "1 1/"+step+" * * * ? ";
            }else if("day".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("week".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }else if("month".equals(statUnits)){
                cronExpression = "1 1 1/"+step+" * * ? ";
            }
        }
        //test
        cronExpression = "1 * * * * ? ";
        reducer.setCronExpression(cronExpression);
        return cronExpression;
    }


    public static Duration matchDuration(String statPeriod){
        if(StringUtil.isEmpty(statPeriod)){
            return null;
        }

        Duration duration = new Duration();
        if(statPeriod.startsWith("every")){
            String[] recentyArray = statPeriod.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            if("minute".equals(statUnits)){
                int t = Integer.parseInt(statSteps) > 10?Integer.parseInt(statSteps):10;
                long startTime = DateUtil.getAheadMinuteTimeStamp(-1 * t);
                long endTime = DateUtil.getAheadMinuteTimeStamp(0);
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("hour".equals(statUnits)){
                long startTime = DateUtil.getAheadHourTimeStamp(-1 * Integer.parseInt(statSteps)); //default last day
                long endTime = DateUtil.getAheadHourTimeStamp(0);
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("day".equals(statUnits)){
                long startTime = DateUtil.getAheadDayTimeStamp(-1 * Integer.parseInt(statSteps)); //default last day
                long endTime = DateUtil.getAheadDayTimeStamp(0);
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("week".equals(statUnits)){
                long startTime = DateUtil.getAheadWeekTimeStamp(-1 * Integer.parseInt(statSteps)); //default last week
                long endTime = DateUtil.getAheadWeekTimeStamp(0);
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("month".equals(statUnits)){
                long startTime = DateUtil.getAheadMonthTimeStamp(-1 * Integer.parseInt(statSteps));
                long endTime = DateUtil.getAheadMonthTimeStamp(0);
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }
        }else if(statPeriod.startsWith("recenty")){
            String[] recentyArray = statPeriod.split("-");
            String statSteps = recentyArray[1];
            String statUnits = recentyArray[2];
            if("minute".equals(statUnits)){
                long startTime = DateUtil.getAheadRecentyMinuteTimeStamp(new Date(), Integer.parseInt(statSteps)>10?Integer.parseInt(statSteps):10);
                long endTime = DateUtil.getTimeStamp(new Date());
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("hour".equals(statUnits)){
                long startTime = DateUtil.getAheadRecentyHourTimeStamp(new Date(), Integer.parseInt(statSteps));
                long endTime = DateUtil.getTimeStamp(new Date());
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("day".equals(statUnits)){
                long startTime = DateUtil.getAheadRecentyDayTimeStamp(new Date(),Integer.parseInt(statSteps));
                long endTime = DateUtil.getTimeStamp(new Date());
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("week".equals(statUnits)){
                long startTime = DateUtil.getAheadRecentyDayTimeStamp(new Date(),Integer.parseInt(statSteps)*7);
                long endTime = DateUtil.getTimeStamp(new Date());
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }else if("month".equals(statUnits)){
                long startTime = DateUtil.getAheadRecentyDayTimeStamp(new Date(), Integer.parseInt(statSteps)*30);
                long endTime = DateUtil.getTimeStamp(new Date());
                duration.setStartTime(startTime);
                duration.setEndTime(endTime);
            }
        }
        return duration;
    }

    public static void main(String [] args){
        Duration duration = JobKits.matchDuration("every-3-hour");
        System.out.println("start:"+duration.getStartTime());
        System.out.println("end:"+duration.getEndTime());
    }
}
